//
//  WaterfallCell.m
//  News
//
//  Created by Han on 15-4-15.
//  Copyright (c) 2015年 IPingLun. All rights reserved.
//

#import "WaterfallCell.h"

@implementation WaterfallCell

#define WaterfallCellTitleFont [UIFont systemFontOfSize:12.0]
#define WaterfallCellCommentFont [UIFont systemFontOfSize:10.0]
#define WaterfallCellTitleWidth WATERFALL_WIDTH - 4
#define WaterfallCellCommentWidth WATERFALL_WIDTH - 4
#define WaterfallCellPaddingHeight 18 + 26

@synthesize newsImg, newsTitle, comment1, comment2, comment3;

- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        //self.layer.borderWidth = 1;
        //self.layer.borderColor = [UIColorFromRGB(0xD3D2D4) CGColor];
        
        newsImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder_image_200_150"]];
        newsImg.contentMode = UIViewContentModeScaleAspectFill;
        newsImg.clipsToBounds = YES;
        [self addSubview:newsImg];
        
        [newsImg makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.top).offset(0);
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.height.equalTo(WATERFALL_WIDTH * 3/4);
        }];
        
        UIView *divideLine1 = UIView.new;
        divideLine1.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:divideLine1];
        
        [divideLine1 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.bottom.equalTo(newsImg.bottom).offset(0);
            make.height.equalTo(0.5);
            
        }];
        
        
        newsTitle = [UILabel new];
        //newsTitle.backgroundColor = [UIColor greenColor];
        newsTitle.font = WaterfallCellTitleFont;
        newsTitle.textColor = MAIN_COLOR;
        newsTitle.numberOfLines = 0;
        [self addSubview:newsTitle];
        
        [newsTitle makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(3);
            make.right.equalTo(self.right).offset(-1);
            make.top.equalTo(newsImg.bottom).offset(2);  // 间距 2
            //make.height.equalTo(10);
            //make.bottom.equalTo(divideLine5.top);
        }];

        
        
        UIView *divideLine2 = UIView.new;
        divideLine2.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:divideLine2];
        
        [divideLine2 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.bottom.equalTo(newsTitle.bottom).offset(2); // 间距 2
            make.height.equalTo(0.5);

        }];
        
        
        comment1 = [UILabel new];
        comment1.font = WaterfallCellCommentFont;
        comment1.textColor = [UIColor blackColor];
        comment1.numberOfLines = 0;
        [self addSubview:comment1];
        
        [comment1 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(3);
            make.right.equalTo(self.right).offset(-1);
            make.top.equalTo(divideLine2.bottom).offset(2);  // 间距 2
        }];
        
        
        UIView *divideLine3 = UIView.new;
        divideLine3.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:divideLine3];
        
        [divideLine3 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.bottom.equalTo(comment1.bottom).offset(2); // 间距 2
            make.height.equalTo(0.5);
            
        }];
        
        comment2 = [UILabel new];
        comment2.font = WaterfallCellCommentFont;
        comment2.textColor = [UIColor blackColor];
        comment2.numberOfLines = 0;
        [self addSubview:comment2];
        
        [comment2 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(3);
            make.right.equalTo(self.right).offset(-1);
            make.top.equalTo(divideLine3.bottom).offset(2);  // 间距 2
        }];
        
        
        UIView *divideLine4 = UIView.new;
        divideLine4.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:divideLine4];
        
        [divideLine4 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.bottom.equalTo(comment2.bottom).offset(2); // 间距 2
            make.height.equalTo(0.5);
            
        }];
        
        
        comment3 = [UILabel new];
        comment3.font = WaterfallCellCommentFont;
        comment3.textColor = [UIColor blackColor];
        comment3.numberOfLines = 0;
        [self addSubview:comment3];
        
        [comment3 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(3);
            make.right.equalTo(self.right).offset(-1);
            make.top.equalTo(divideLine4.bottom).offset(2);  // 间距 2
        }];
        
        
        UIView *divideLine5 = UIView.new;
        divideLine5.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:divideLine5];
        
        [divideLine5 makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.bottom.equalTo(comment3.bottom).offset(2); // 间距 2
            make.height.equalTo(0.5);
        }];
        
        
        UIView *bottomLine = UIView.new;
        bottomLine.backgroundColor = UIColorFromRGB(0xD3D2D4);
        [self addSubview:bottomLine];
        
        [bottomLine makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.bottom.equalTo(self.bottom).offset(0);
            make.height.equalTo(2); // 间距 2
        }];
        
        // 间距 26
        
        
        UIView *verticalLine1 = UIView.new;
        verticalLine1.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:verticalLine1];
        
        [verticalLine1 makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(divideLine5.top).offset(0);
            make.right.equalTo(self.right).offset(-36);
            make.bottom.equalTo(bottomLine.top).offset(0);
            make.width.equalTo(0.5);
        }];
        
        
        //点赞按钮
        UIButton *likeButton = UIButton.new;
        likeButton.exclusiveTouch = YES;
        [likeButton setImage:[UIImage imageNamed:@"dashboard_recommand_off_default"] forState:UIControlStateNormal];
        likeButton.imageEdgeInsets = UIEdgeInsetsMake(4.5, 11, 5.5, 11);
        //[likeButton addTarget:self action:@selector(menuToggle) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:likeButton];
        
        [likeButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(verticalLine1.right).offset(0);
            make.right.equalTo(self.right).offset(0);
            make.top.equalTo(divideLine5.top).offset(0);
            make.bottom.equalTo(bottomLine.top).offset(0);
        }];
        
        UIView *verticalLine2 = UIView.new;
        verticalLine2.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:verticalLine2];
        
        [verticalLine2 makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(divideLine5.top).offset(0);
            make.right.equalTo(verticalLine1.left).offset(-36);
            make.bottom.equalTo(bottomLine.top).offset(0);
            make.width.equalTo(0.5);
        }];
        
        //分享按钮
        UIButton *shareButton = UIButton.new;
        shareButton.exclusiveTouch = YES;
        [shareButton setImage:[UIImage imageNamed:@"dashboard_reblog_default"] forState:UIControlStateNormal];
        shareButton.imageEdgeInsets = UIEdgeInsetsMake(5.5, 10.7, 5.5, 10.7);
        //[likeButton addTarget:self action:@selector(menuToggle) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:shareButton];
        
        [shareButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(verticalLine2.right).offset(0);
            make.right.equalTo(verticalLine1.left).offset(0);
            make.top.equalTo(divideLine5.top).offset(0);
            make.bottom.equalTo(bottomLine.top).offset(0);
        }];
        
        
        UIView *verticalLine3 = UIView.new;
        verticalLine3.backgroundColor = UIColorFromRGB(0xE0E0E0);
        [self addSubview:verticalLine3];
        
        [verticalLine3 makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(divideLine5.top).offset(0);
            make.right.equalTo(verticalLine2.left).offset(-36);
            make.bottom.equalTo(bottomLine.top).offset(0);
            make.width.equalTo(0.5);
        }];
        
        //评论按钮
        UIButton *commentButton = UIButton.new;
        commentButton.exclusiveTouch = YES;
        [commentButton setImage:[UIImage imageNamed:@"dashboard_reply_default"] forState:UIControlStateNormal];
        commentButton.imageEdgeInsets = UIEdgeInsetsMake(6.5, 10.7, 4.5, 10.7);
        //[likeButton addTarget:self action:@selector(menuToggle) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:commentButton];
        
        [commentButton makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(verticalLine3.right).offset(0);
            make.right.equalTo(verticalLine2.left).offset(0);
            make.top.equalTo(divideLine5.top).offset(0);
            make.bottom.equalTo(bottomLine.top).offset(0);
        }];
        
        UILabel *sourceLabel = UILabel.new;
        sourceLabel.text = @"网易";
        sourceLabel.font = [UIFont systemFontOfSize:12.0];
        sourceLabel.textColor = UIColorFromRGB(0x999999);
        sourceLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:sourceLabel];
        
        
        [sourceLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.left).offset(0);
            make.right.equalTo(verticalLine3.left).offset(0);
            make.top.equalTo(divideLine5.top).offset(0);
            make.bottom.equalTo(bottomLine.top).offset(0);
        }];
        
        
    }
    return self;
}


- (void)setNews:(News *)news {
    
    newsTitle.text = news.title;
    //comment1.text = news.comment1;
    
    NSMutableAttributedString *comment1AttributedString = [[NSMutableAttributedString alloc] initWithString:news.comment1];
    [comment1AttributedString addAttribute:NSForegroundColorAttributeName value:MAIN_COLOR range:NSMakeRange(0,8)];

    comment1.attributedText = comment1AttributedString;
    
    
    comment2.text = news.comment2;
    comment3.text = news.comment3;
    
    // 淡入效果
    [newsImg sd_setImageWithURL:[NSURL URLWithString:news.img_url]
               placeholderImage:[UIImage imageNamed:@"placeholder_image_200_150"] options:SDWebImageCacheMemoryOnly progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                   //NSLog(@"%.2f", (float)receivedSize / (float)expectedSize);
               }
                      completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                          if (image && cacheType == SDImageCacheTypeNone)
                          {
                              newsImg.alpha = 0.0;
                              [UIView animateWithDuration:0.3
                                               animations:^{
                                                   newsImg.alpha = 1.0;
                                               }]; 
                          }
                      }];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    newsTitle.preferredMaxLayoutWidth = WaterfallCellTitleWidth;
    comment1.preferredMaxLayoutWidth = WaterfallCellCommentWidth;
    comment2.preferredMaxLayoutWidth = WaterfallCellCommentWidth;
    comment3.preferredMaxLayoutWidth = WaterfallCellCommentWidth;
    
    [super layoutSubviews];
}


/*
+ (float)getHeight:(NSString *)text {
    
    UILabel *label = [UILabel new];
    label.text = text;
    label.font = WaterfallCellTitleFont;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    
    
    CGSize size = CGSizeMake(WaterfallCellTitleWidth, 0);
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:WaterfallCellTitleFont, NSFontAttributeName, nil];
    
    return [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil].size.height;

}
*/
 

+ (float)getHeight:(NSString *)title :(NSString *)comment1 :(NSString *)comment2 :(NSString *)comment3 {
    
    float height = 0.0;
    
    // 标题
    UILabel *label = [UILabel new];
    label.text = title;
    label.font = WaterfallCellTitleFont;
    label.numberOfLines = 0;
    label.lineBreakMode = NSLineBreakByTruncatingTail;
    CGSize size = [label boundingRectWithSize:CGSizeMake(WaterfallCellTitleWidth, CGFLOAT_MAX)];
    
    height += size.height;
    
    // 评论1
    UILabel *comment = [UILabel new];
    comment.text = comment1;
    comment.font = WaterfallCellCommentFont;
    comment.numberOfLines = 0;
    comment.lineBreakMode = NSLineBreakByTruncatingTail;
    size = [comment boundingRectWithSize:CGSizeMake(WaterfallCellCommentWidth, CGFLOAT_MAX)];
    
    height += size.height;
    
    // 评论2
    comment.text = comment2;
    size = [comment boundingRectWithSize:CGSizeMake(WaterfallCellCommentWidth, CGFLOAT_MAX)];
    
    height += size.height;
    
    // 评论3
    comment.text = comment3;
    size = [comment boundingRectWithSize:CGSizeMake(WaterfallCellCommentWidth, CGFLOAT_MAX)];
    
    height += size.height;
    
    return height + WaterfallCellPaddingHeight + WATERFALL_WIDTH * 3/4;
    
}


@end
